package com.ssyt.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.company.service.ApplicationService;

public class ApplicationServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ApplicationService applicationService = new ApplicationService();

	public ApplicationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("listApplication".equals(command)) {
			listApplication(request, response);
		}
	}

	/**
	 * 查询所有的申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listApplication(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 分页信息
		int pageNo = Constants.DEFAULT_PAGE_NO;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr != null) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		// 条件信息
		String positionName = request.getParameter("positionName");
		int status = 0;
		String statusStr = request.getParameter("status");
		if (StringUtils.isNotBlank(statusStr)) {
			status = Integer.parseInt(statusStr);
		}
		String appTimeStart = request.getParameter("appTimeStart");
		String appTimeEnd = request.getParameter("appTimeEnd");

		ApplicationEntity application = new ApplicationEntity();
		application.setPosition$name(positionName);
		application.setStatus(status);
		application.setAppTimeStart(appTimeStart);
		application.setAppTimeEnd(appTimeEnd);
		PageModel<ApplicationEntity> pageModel = applicationService.list(
				application, pageNo, pageSize);
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("application", application);
		request.getRequestDispatcher("/WEB-INF/view/company/main.jsp").forward(
				request, response);
	}
}
